; (function () {
	window.projectConfigIndex = new Vue({
		el: '#projectConfig',
		data: {
			showCompany: false,
			projectId: hlUrlUtil.GetQueryString('projectId'),
			proejctStatusList: [
				{name: '运营中', code: 'FN'},
				{name: '筹备中', code: 'WF'}
			],
			data: {
				projectName: '',
				status: '',
				projectFdName: '',
				selCompany: '',
				companyId: '',
				provinceSel: '',
				citySel: '',
				areaSel: '',
				provinceId: '',
				cityId: '',
				areaId: '',
				address: '',
				imgUrl: '',
				zdAreage: '',
				jzAreage: '',
				czAreage: '',
				owner: '',
				name: '',
				phone: '',
				equity: '',
				tdTime: '',
				wyTime: '',
				survey: '',
				pcompany: '',
				fpersonName: '',

				companyRegistAddress:'',
				companyContactName:'',
				companyContactPhone:'',
				companyManager:'',

				bankInfos: [
					{name: '', account: '', bankAccount: ''}
				],
				location: '116.397399,39.909209'
			},
			tsflag: ['Y'],
			istsFlag: '',
			selectedData: [{name: '推送物业系统', code: 'Y'}],
			manualLocation: '',
			manualFlag: false,
			companysList: [],
			provinceList: [],
			cityList: [],
			areaList: [],
			auth: JSON.parse(localStorage.getItem('permissions'))
		},
		methods: {
			isShowyy () {
				hlhttp.get({
					url: 'configure/dict',
					data: {
					  module_code: 'CMMN',
					  config_name: 'PUSH_PROPERTY_SYSTEM_CONFIG'
					},
					success: (data) => {
						console.log(data)
						this.istsFlag = [data[0].dictValue]
					}
				})
			},
			tsflagClcik () {
				this.tsflag = !this.tsflag
			},
			showCompanyTab () {
				this.showCompany = !this.showCompany
			},
			//设置项目状态
			setProjectStatus (data) {
				this.data.status = data.code;
			},
			//关联公司list
			companysData () {
				hlhttp.get({
					url: 'companys',
					success: (data) => {
						this.companysList = data;
					}
				})
			},
			//设置关联公司
			setCompany (data) {
				this.data.companyId = data.compayId;
				this.data.selCompany = data.shortName;
			},
			//省
			provinceData () {
				hlhttp.get({
					url: 'sub_regions',
					data: {region_id: '1'},
					success: (data) => {
						this.provinceList = data;
					}
				})
			},
			//设置省
			setProvince (data) {
				this.data.provinceSel = data.name;
				this.data.provinceId = data.regionId;
				this.data.citySel = '';
				this.data.areaSel ='';
				this.data.address = ''
				this.cityData()
				this.areaList = [];
				this.$refs.citySel.innerHTML = '市';
				this.$refs.areaSel.innerHTML = '区';
			},
			//市
			cityData () {
				hlhttp.get({
					url: 'sub_regions',
					data: {region_id: this.data.provinceId},
					success: (data) => {
						this.cityList = data;
					}
				})
			},
			setCity (data) {
				this.data.citySel = data.name;
				this.data.cityId = data.regionId;
				this.data.areaSel ='';
				this.data.address = ''
				this.areaData()
				this.$refs.areaSel.innerHTML = '区';
			},
			//区
			areaData () {
				hlhttp.get({
					url: 'sub_regions',
					data: {region_id: this.data.cityId},
					success: (data) => {
						this.areaList = data;
					}
				})
			},
			setArea (data) {
				this.data.areaSel = data.name;
				this.data.address = ''
				this.data.areaId = data.regionId;
			},
			//上传图片
			updateImg () {
				document.querySelector('.image-up').click()
			},
			//map
			mapGaoge () {
				console.log(this.data.location);
				let center = this.data.location ? this.data.location.split(',') : null
				let map = new AMap.Map('mapGaode', {
				  resizeEnable: true,
					center: center,
				  zoom: 14
				});
				var marker = new AMap.Marker({
						position: new AMap.LngLat(map.getCenter().lng, map.getCenter().lat),
						icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
						offset: new AMap.Pixel(-10, -20)
				});
				 map.add(marker);
			},
			// update Map
			updateMap () {
				let map = new AMap.Map('mapGaode', {
				  resizeEnable: true,
				  zoom: 14
				});

				var marker = new AMap.Marker({
						position: new AMap.LngLat(map.getCenter().lng, map.getCenter().lat),
						icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
						offset: new AMap.Pixel(-10, -20)
				});
				map.add(marker);
				this.data.location = map.getCenter().lng +','+ map.getCenter().lat
			},
			newMap () {

				hlhttp.get({
					baseUrl: 'https://restapi.amap.com/v3/',
					url: 'geocode/geo',
					headers: {},
					dataType: "jsonp", //指定服务器返回的数据类型
					data: {address: this.data.provinceSel + this.data.citySel + this.data.areaSel + this.data.address, key: '9af309733756a2501ee4cc050d11a69b'},
					success: (data) => {
						if (data.geocodes.length) {
							var lnglat = data.geocodes[0].location;
							this.manualLocation = lnglat;
							this.usePosition()
						}
						if (!data.geocodes.length) {
							this.updateMap()
						}
					}
				})
			},
			//manual gaode
			manualGaode () {
				let center = [116.43,39.92]
				if (this.data.location) {
					center = JSON.parse(JSON.stringify(this.data.location)).split(',')
				}
				var map = new AMap.Map("newGaode", {
					resizeEnable: true,
					center: center,
					zoom: 14
				});
				var marker = new AMap.Marker({
						position: new AMap.LngLat(map.getCenter().lng, map.getCenter().lat),
						icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
						offset: new AMap.Pixel(-10, -20)
				});
				map.add(marker);
				this.manualLocation = map.getCenter().lng+','+map.getCenter().lat;
				map.on('click', (e) => {
					this.manualLocation = e.lnglat.getLng()+','+e.lnglat.getLat();
					map.clearMap();
					var marker = new AMap.Marker({
						position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()),
						icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
						offset: new AMap.Pixel(-10, -20)
					});
					map.add(marker);
				});
			},
			//show newMap
			showNewMap () {
				this.manualFlag = true;
				setTimeout(() => {
					this.manualGaode()
				}, 30)
			},
			// hide newMap
			hideNewMap () {
				this.manualFlag = false;
			},
			//使用当前定位
			usePosition () {
				this.data.location = JSON.parse(JSON.stringify(this.manualLocation))
				this.hideNewMap()
				this.mapGaoge()
			},
			//验证开户账号
			bankAccountValidate (i) {
				if (this.data.bankInfos[i].bankAccount.length > 29) {
					hl.toast('账户位数不能大于29位数字')
					this.data.bankInfos[i].bankAccount = ''
					return
				}
				if (this.data.bankInfos[i].bankAccount.length < 9) {
					hl.toast('账户位数不能小于9位数字')
					this.data.bankInfos[i].bankAccount = ''
					return
				}
				const regxbx = /^[\-\(\)\{\}\[\]0-9]+$/;
				if (!regxbx.test(this.data.bankInfos[i].bankAccount)) {
				  this.data.bankInfos[i].bankAccount = ''
				  hl.toast('账户填写错误，请重新填写')
				  return
				}
			},
			//新增收款信息
			addBanks () {
				let flag = false;
				this.data.bankInfos.map(item => {
					if (!item.name) {
						flag = true;
					}
					if (!item.account) {
						flag = true;
					}
					if (!item.bankAccount) {
						flag = true;
					}
				})
				if (flag) {
					hl.toast('请先完善当前的收款信息')
					return
				}
				this.data.bankInfos.push({name: '', account: '', bankAccount: ''})
			},
			//提交验证
			validate () {
				if ( !this.data.projectName ) {
					hl.toast('请填写项目名称')
					return false;
				}
				if ( !this.data.status ) {
					hl.toast('请选择项目状态')
					return false;
				}
				if ( !this.data.companyId ) {
					hl.toast('请选择关联公司')
					return false;
				}
				if ( !this.data.provinceId ) {
					hl.toast('请选择项目所在地')
					return false;
				}
				if ( !this.data.cityId ) {
					hl.toast('请选择项目所在地')
					return false;
				}
				if ( !this.data.areaId ) {
					hl.toast('请选择项目所在地')
					return false;
				}
				if ( !this.data.address ) {
					hl.toast('请填写详细地址')
					return false;
				}
				if ( this.data.location.length == 0 ) {
					hl.toast('请选择地理位置')
					return false;
				}
				if ( !this.data.imgUrl ) {
					hl.toast('请上传项目预览图')
					return false;
				}
				if (this.showCompany) {
					if ( !this.data.pcompany ) {
						hl.toast('请填写项目公司全称')
						return false;
					}
					if ( !this.data.fpersonName ) {
						hl.toast('请填写法人')
						return false;
					}
					let flag = true;
					this.data.bankInfos.map(item => {
						if (!item.name || !item.account || !item.bankAccount) {
							flag = false
						}
					})
					if ( !flag ) {
						hl.toast('请填写收款信息')
						return false;
					}
				}
				return true;
			},
			//编辑项目
			editProject () {
				if (!this.validate()) return;
				let accountNames = [], banksNames = [], bankAccounts = []
				if ( this.showCompany ) {
					this.data.bankInfos.map(item => {
						accountNames.push(item.name)
						banksNames.push(item.account)
						bankAccounts.push(item.bankAccount)
					})
				}

				hlhttp.put({
					url: 'project',
					data: {
						project_id: this.projectId,
						name: this.data.projectName,
						config_state: this.data.status,
						city_id: this.data.cityId,
						city_name: this.data.citySel,
						province_id: this.data.provinceId,
						province_name: this.data.provinceSel,
						district_id: this.data.areaId,
						district_name: this.data.areaSel,
						address: this.data.address,
						rperson_name: this.data.name || '#',
						rperson_phone: this.data.phone || '#',
						preview: this.data.imgUrl,
						company_id: this.data.companyId,
						company_name: this.data.selCompany,
						pcompany: this.data.pcompany || '#',
						fperson_name: this.data.fpersonName || '#',
						company_regist_address:this.data.companyRegistAddress || '#',
						company_contact_name:this.data.companyContactName || '#',
						company_contact_phone:this.data.companyContactPhone || '#',
						company_manager:this.data.companyManager || '#',
						fperson_phone: '#',
						banks_names: banksNames.toString() || '#',
						bank_accounts: bankAccounts.toString() || '#',
						account_names: accountNames.toString() || '#',
						wy_profile: this.data.survey || '#',
						zd_acreage: this.data.zdAreage || '#',
						jz_acreage: this.data.jzAreage || '#',
						cz_acreage: this.data.czAreage || '#',
						owner: this.data.owner || '#',
						land_end_date: this.data.tdTime || '#',
						complete_date: this.data.wyTime || '#',
						income_percent: this.data.equity || '#',
						legal_name: this.data.projectFdName || '#',
						location: this.data.location,
						is_push_property_flag: this.istsFlag == 'Y' ? this.tsflag.length ? 'Y' : 'N' : '#'
					},
					success: (data) => {
						hl.toast('项目编辑成功')
						window.projectConfigTab.projectName = this.data.projectName;
						sessionStorage.setItem('projectName', this.data.projectName)
					}
				})
			},
			//add project
			addProject () {
				if (!this.validate()) return;
				let accountNames = [], banksNames = [], bankAccounts = []
				if ( this.showCompany ) {
					this.data.bankInfos.map(item => {
						accountNames.push(item.name)
						banksNames.push(item.account)
						bankAccounts.push(item.bankAccount)
					})
				}
				hlhttp.post({
					url: 'project',
					data: {
						name: this.data.projectName,
						config_state: this.data.status,
						city_id: this.data.cityId,
						city_name: this.data.citySel,
						province_id: this.data.provinceId,
						province_name: this.data.provinceSel,
						district_id: this.data.areaId,
						district_name: this.data.areaSel,
						address: this.data.address,
						rperson_name: this.data.name || '#',
						rperson_phone: this.data.phone || '#',
						preview: this.data.imgUrl,
						company_id: this.data.companyId,
						company_name: this.data.selCompany,
						pcompany: this.data.pcompany || '#',
						fperson_name: this.data.fpersonName || '#',
						company_regist_address:this.data.companyRegistAddress || '#',
						company_contact_name:this.data.companyContactName || '#',
						company_contact_phone:this.data.companyContactPhone || '#',
						company_manager:this.data.companyManager || '#',
						fperson_phone: '#',
						banks_names: banksNames.toString() || '#',
						bank_accounts: bankAccounts.toString() || '#',
						account_names: accountNames.toString() || '#',
						wy_profile: this.data.survey || '#',
						zd_acreage: this.data.zdAreage || '#',
						jz_acreage: this.data.jzAreage || '#',
						cz_acreage: this.data.czAreage || '#',
						owner: this.data.owner || '#',
						land_end_date: this.data.tdTime || '#',
						complete_date: this.data.wyTime || '#',
						income_percent: this.data.equity || '#',
						legal_name: this.data.projectFdName || '#',
						location: this.data.location,
						is_push_property_flag: this.tsflag.length ? 'Y' : 'N',
						expansion_proj_id: JSON.parse(unescape(hlUrlUtil.GetQueryString('data'))) ? JSON.parse(unescape(hlUrlUtil.GetQueryString('data'))).id : '#'
					},
					success: (data) => {
						hl.toast('项目新增成功')
						hlUrlUtil.addURLParam({projectId: data.id}),
						window.projectConfigTab.active = '楼宇楼层配置';
						window.projectConfigTab.projectName = this.data.name;
						window.projectConfigTab.menuShow()

						sessionStorage.setItem('projectPage', window.projectConfigTab.active)
						sessionStorage.setItem('projectName', this.data.name)
					}
				})
			},
			//获取项目详情
			projectData () {
				hlhttp.get({
					url: 'project',
					data: {project_id: this.projectId},
					success: (data) => {
						this.data = {
							projectName: data.name,
							status: data.configState,
							projectFdName: data.legalName,
							selCompany: data.companyName,
							companyId: data.companyId,
							provinceSel: data.provinceName,
							citySel: data.cityName,
							areaSel: data.districtName,
							provinceId: data.provinceId !== '0' ? data.provinceId : null,
							cityId: data.cityId !== '0' ? data.cityId : null,
							areaId: data.districtId !== '0' ? data.districtId : null,
							address: data.address,
							imgUrl: data.preview,
							zdAreage: data.zdAcreage,
							jzAreage: data.jzAcreage,
							czAreage: data.czAcreage,
							owner: data.owner,
							name: data.rpersonName,
							phone: data.rpersonPhone,
							equity: data.incomePercent,
							tdTime: hlDateUtil.formatUTCDate(data.landEndDate, 'yyyy-MM-dd'),
							wyTime: hlDateUtil.formatUTCDate(data.completeDate, 'yyyy-MM-dd'),
							survey: data.wyProfile,
							pcompany: data.pcompany,
							fpersonName: data.fpersonName,
							companyRegistAddress:data.companyRegistAddress || '',
							companyContactName:data.companyContactName || '',
							companyContactPhone:data.companyContactPhone || '',
							companyManager:data.companyManager || '',
							bankInfos: [],
							location: data.location || []
						}

						console.log(this.data)

						this.cityData()
						this.areaData()
						this.mapGaoge()

						this.$refs.proejctStatus.innerHTML = data.configState == 'FN' ? '运营中' : '筹备中';
						if (data.isPushPropertyFlag) {
							this.tsflag = [data.isPushPropertyFlag]
						}
						if(data.companyName) this.$refs.selCompany.innerHTML = data.companyName ;
						if(data.provinceName) this.$refs.provinceSel.innerHTML = data.provinceName;
						if(data.cityName) this.$refs.citySel.innerHTML = data.cityName;
						if(data.districtName) this.$refs.areaSel.innerHTML = data.districtName;

						window.projectConfigTab.projectName = data.name;
						sessionStorage.setItem('projectName', data.name)

						if (data.accountNames && data.accountNames.length) {
							this.showCompany = true;
							data.accountNames.split(',').map((item, index) => {
								this.data.bankInfos.push({
									name: item,
									account: data.bankNames.split(',')[index],
									bankAccount: data.bankAccounts.split(',')[index]
								})
							})
						}
					}
				})
			},
			//删除银行账号
			delBank (i) {
				this.data.bankInfos.splice(i, 1)
			},
			//转运营
			runOperate () {
				let data = JSON.parse(unescape(hlUrlUtil.GetQueryString('data')))
				this.data.projectName = data.projName;
				this.data.projectFdName = data.projLegalName;
				this.data.companyId = data.companyId;
				this.data.provinceSel = data.provinceName;
				this.data.citySel = data.cityName;
				this.data.areaSel = data.districtName;
				this.data.provinceId = data.provinceId;
				this.data.cityId = data.cityId;
				this.data.areaId = data.districtId;
				this.data.address = data.address;
				this.data.zdAreage = data.areaCoveredAcreage;
				this.data.jzAreage = data.buildingCoveredAcreage;
				this.data.czAreage = data.rentAcreage;
				this.data.owner = data.projPrOwnerName;
				this.data.name = data.contactName;
				this.data.phone = data.contactPhoneNum;
				this.data.equity = data.rightsInterestsRates;
				this.data.tdTime = hlDateUtil.formatUTCDate(data.landUseRightsExpireDate, 'yyyy-MM-dd');
				this.data.wyTime = hlDateUtil.formatUTCDate(data.propertyAccessionDate, 'yyyy-MM-dd');
				this.data.survey = data.propertyProfile;
				if (data.companyFullName || data.legalPersonName) {
					this.showCompany = true;
					this.data.pcompany = data.companyFullName;
					this.data.fpersonName = data.legalPersonName;
				}
				this.$refs.selCompany.innerHTML = data.companyName ? data.companyName : '请选择';
				this.$refs.provinceSel.innerHTML = data.provinceName ? data.provinceName : '请选择';
				this.$refs.citySel.innerHTML = data.cityName ? data.cityName : '请选择';
				this.$refs.areaSel.innerHTML = data.districtName ? data.districtName : '请选择';
			}
		},
		created () {
			hlSelectUi()
		},
		mounted () {
			if ( this.projectId ) {
				this.projectData()
			}
			if (hlUrlUtil.GetQueryString('market')) {
				this.runOperate()
			}
			this.companysData()
			this.isShowyy()
			this.mapGaoge()
			this.provinceData()
			imgupload0('.image-up', (data) => {
				this.data.imgUrl = data.json.url
			})
		}
	})
} ())
